<?php
namespace App\Services;
use Exception,Log;
use App\Models\Miniappsauthorization;
use App\Models\Linkslistsminapps;
use App\Services\WechatopenService;
use App\Services\WechatwqopenService;
use App\Services\ThirdapiService;
use App\Services\MiniappService;

class LinkstokenService
{
    public static function getAccessToken($request,$appid)
    {
        if (in_array($request->miniapp_type, [1,4])) {
            $componentAppids = [1=>["wxa755405206410fd2"],4=>["wxea6913abfa9210de"]];
            // 先获取三方上架的小程序
            $miniapp = Miniappsauthorization::getMiniappAuthorization($appid,$componentAppids[$request->miniapp_type]);
            if (empty($miniapp)) {
                $miniapp = Miniappsauthorization::getMiniappsAuthorizationSimple($appid);
            }
            if (empty($miniapp)) return ["status"=>"error","code"=>1002,"msg"=>"小程序不存在,无法使用!"];
            try {
                // 获取Token信息
                if ($request->miniapp_type == 1) {
                    // 天天外链授权上架的小程序
                    $access_token = WechatopenService::getMiniProgramAccessToken($miniapp["component_appid"],$miniapp["authorizer_appid"],$miniapp["authorizer_refresh_token"]);
                } else {
                    if ($miniapp["component_appid"] == "wxa755405206410fd2") {
                        $access_token = WechatopenService::getMiniProgramAccessToken($miniapp["component_appid"],$miniapp["authorizer_appid"],$miniapp["authorizer_refresh_token"]);
                    } else {
                        // 扫码授权自己的微信小程序
                        $access_token = WechatwqopenService::getMiniProgramAccessToken($miniapp["authorizer_appid"],$miniapp["authorizer_refresh_token"]);
                    }
                }
                return ["status"=>"success","access_token"=>$access_token];
            } catch (Exception $e) {
                $message = $e->getMessage();
                $message = json_decode(str_replace("Request access_token fail: ", "", $message));
                if (empty($message)) return ["status"=>"error","code"=>1001,"msg"=>"小程序异常,请联系客服！"];
                if ($message->errcode == 40013) return ["status"=>"error","code"=>1001,"msg"=>"小程序Appid错误,请确认Appid正确且小程序已上线！"];
                if ($message->errcode == 40125) return ["status"=>"error","code"=>1001,"msg"=>"小程序Appsecret错误,请修改后重新提交！"];
                if ($message->errcode == 40001) return ["status"=>"error","code"=>1001,"msg"=>"小程序未授权！"];
                if ($message->errcode == 50002) return ["status"=>"error","code"=>1001,"msg"=>"小程序已被注销或封禁,请修改小程序！"];
                if ($message->errcode == 61003) return ["status"=>"error","code"=>1001,"msg"=>"小程序未授权给天天外链！"];
                if ($message->errcode == 61023) {
                    Log::info("getAccessToken-----------");
                    Log::info(json_encode($message));
                    Log::info(json_encode($message));
                    Log::info("getAccessToken-----------");
                    // try {
                    //     WechatopenService::getMiniProgramAccessToken($miniapp["component_appid"],$miniapp["authorizer_appid"],$miniapp["authorizer_refresh_token"]);
                    //     Log::info("强制刷新成功!");
                    // } catch (Exception $e) {
                    //     Log::info($e);
                    // }
                    return ["status"=>"error","code"=>1001,"msg"=>"小程序授权过期,请重新授权！"];
                }
                return ["status"=>"error","code"=>1001,"msg"=>"小程序异常,请联系客服！"];
            }
        } else {
            $miniapp = Linkslistsminapps::getLinksListsMinappsByAppidSimple($appid);
            if (empty($miniapp)) return ["status"=>"error","code"=>1002,"msg"=>"小程序不存在,无法使用!"];
            if ($miniapp->disable == 2 || $miniapp->disable == 4) return ["status"=>"error","code"=>1002,"msg"=>"小程序不存在,无法使用!"];
            // 通过API方式接入
            if ($request->miniapp_type == 3) {
                $AccessToken = ThirdapiService::apiGetThirdAccessToken($appid);
                if ($AccessToken["status"] == "success") return ["status"=>"success","access_token"=>$AccessToken["access_token"]];
            } else {
                $AccessToken = MiniappService::miniappAccessToken($appid,$miniapp->secret);
                if (isset($AccessToken["access_token"])) return ["status"=>"success","access_token"=>$AccessToken["access_token"]];
                if ($AccessToken["errcode"] == 40013) return ["status"=>"error","code"=>40013,"msg"=>"小程序Appid错误,请确认Appid正确且小程序已上线！"];
                if ($AccessToken["errcode"] == 40001) return ["status"=>"error","code"=>40001,"msg"=>"AppSecret 错误或者 AppSecret 不属于这个小程序！"];
                if ($AccessToken["errcode"] == 40125) return ["status"=>"error","code"=>40125,"msg"=>"小程序Appsecret错误,请修改后重新提交！"];
                if ($AccessToken["errcode"] == 50002) return ["status"=>"error","code"=>50002,"msg"=>"小程序已被注销或封禁,请修改小程序！"];
                if ($AccessToken["errcode"] == 40164) return ["status"=>"error","code"=>40164,"msg"=>"小程序未设置IP白名单,请在小程序后台关闭IP限制！"];
                if ($AccessToken["errcode"] == 41004) return ["status"=>"error","code"=>41004,"msg"=>"小程序密钥错误!"];
                return ["status"=>"error","code"=>40013,"msg"=>"小程序出现其他错误3！"];
            }
        }
    }

    public static function forthAccessToken($appid,$miniapp_type)
    {
        try {
            if (in_array($miniapp_type, [1,4])) {
                // 先获取三方上架的小程序
                $componentAppids = [1=>["wxa755405206410fd2"],4=>["wxea6913abfa9210de"]];
                $miniapp = Miniappsauthorization::getMiniappAuthorization($appid,$componentAppids[$miniapp_type]);
                if (empty($miniapp)) return ["status"=>"error","msg"=>"小程序不存在,无法使用!"];
                try {
                    if ($miniapp_type == 1) {
                        // 天天外链授权上架的小程序
                        $access_token = WechatopenService::getMiniProgramAccessToken($miniapp["component_appid"],$miniapp["authorizer_appid"],$miniapp["authorizer_refresh_token"]);
                    } else {
                        // 扫码授权自己的微信小程序
                        $access_token = WechatwqopenService::getMiniProgramAccessTokenForce($miniapp["authorizer_appid"],$miniapp["authorizer_refresh_token"]);
                    }
                    return ["status"=>"success","access_token"=>$access_token];
                 } catch (Exception $e) {
                    $message = $e->getMessage();
                    $message = json_decode(str_replace("Request access_token fail: ", "", $message));
                    if (empty($message)) return ["status"=>"error","msg"=>"小程序异常,请联系客服！"];
                    if ($message->errcode == 40013) return ["status"=>"error","msg"=>"小程序Appid错误,请确认Appid正确且小程序已上线！"];
                    if ($message->errcode == 40125) return ["status"=>"error","msg"=>"小程序Appsecret错误,请修改后重新提交！"];
                    if ($message->errcode == 40001) return ["status"=>"error","msg"=>"小程序未授权！"];
                    if ($message->errcode == 50002) return ["status"=>"error","msg"=>"小程序已被注销或封禁,请修改小程序！"];
                    Log::info(json_encode($miniapp));
                    if ($message->errcode == 61003) return ["status"=>"error","msg"=>"小程序未授权给天天外链！"];
                    if ($message->errcode == 61023) return ["status"=>"error","msg"=>"刷新Token不存在！"];
                    return ["status"=>"error","msg"=>"小程序异常,请联系客服！"];
                }
            } else {
                $miniapp = Linkslistsminapps::getLinksListsMinappsByAppidSimple($appid);
                if (empty($miniapp)) return ["status"=>"error","msg"=>"小程序不存在,无法使用!"];
                if ($miniapp->disable == 2 || $miniapp->disable == 4) return ["status"=>"error","msg"=>"小程序不存在,无法使用!"];
                // 通过API方式接入
                if ($miniapp_type == 3) {
                    $AccessToken = ThirdapiService::apiGetThirdAccessToken($appid);
                    if ($AccessToken["status"] == "success") return ["status"=>"success","access_token"=>$AccessToken["access_token"]];
                } else {
                    $AccessToken = MiniappService::miniappAccessToken($appid,$miniapp->secret,true);
                    if (isset($AccessToken["access_token"])) return ["status"=>"success","access_token"=>$AccessToken["access_token"]];
                    if ($AccessToken["errcode"] == 40013) return ["status"=>"error","code"=>40013,"msg"=>"小程序Appid错误,请确认Appid正确且小程序已上线！"];
                    if ($AccessToken["errcode"] == 40001) return ["status"=>"error","code"=>40001,"msg"=>"AppSecret 错误或者 AppSecret 不属于这个小程序！"];
                    if ($AccessToken["errcode"] == 40125) return ["status"=>"error","code"=>40125,"msg"=>"小程序Appsecret错误,请修改后重新提交！"];
                    if ($AccessToken["errcode"] == 50002) return ["status"=>"error","code"=>50002,"msg"=>"小程序已被注销或封禁,请修改小程序！"];
                    if ($AccessToken["errcode"] == 40164) return ["status"=>"error","code"=>40164,"msg"=>"小程序未设置IP白名单,请在小程序后台关闭IP限制！"];
                    return ["status"=>"error","code"=>40013,"msg"=>"小程序出现其他错误3！"];
                }
            }
        } catch (Exception $e) {
            $message = $e->getMessage();
            Log::info("forthAccessToken-------");
            Log::info($message);
            Log::info($appid);
            Log::info("forthAccessToken-------");
            return ["status"=>"error","msg"=>"小程序异常,无法使用!"];
        }
    }
}
